package com.qch.rabbitmq.demo;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Random;

/**
 * @Author qch
 * @Date 2021/5/13
 */
public class Producer {
    private static String[] LOG_LEVEL={
            "ERROR",
            "FATAL",
            "WARN"
    };
    private static Random random=new Random();

    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //生命direct类型的交换器,交换器和消息队列绑定不需要在这里处理
        channel.exchangeDeclare("ex.routing","direct",false,false,null);
     for (int i = 0; i < 199; i++) {
         String level=LOG_LEVEL[random.nextInt(100)%LOG_LEVEL.length];
            channel.basicPublish("ex.routing",
                    level,null,("这是｛"+level+"｝的消息").getBytes("utf-8"));
        }

        channel.close();
        connection.close();
    }
}
